package com.hanthink.system.service.impl.selfcheck;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hanthink.common.constant.BusinessConstants;
import com.hanthink.system.domain.repair.RepairRecordInfo;
import com.hanthink.system.domain.selfcheck.SelfCheckInfo;
import com.hanthink.system.mapper.repair.RepairRecoedMapper;
import com.hanthink.system.mapper.selfcheck.SelfCheckMapper;
import com.hanthink.system.service.selfcheck.ICheckInfoService;

@Service
public class CheckInfoServiceImpl implements ICheckInfoService {

	@Autowired
	private SelfCheckMapper selfCheckMapper;
	@Autowired
	private RepairRecoedMapper repairRecoedMapper;

	@Override
	public List<SelfCheckInfo> selectNotCheckList(SelfCheckInfo info) {
		info.setStatus(BusinessConstants.SELF_CHECK_STATUS_WAIT);
		return selfCheckMapper.selectNotReviewedSelfCheckList(info);
	}

	@Override
	public List<SelfCheckInfo> selectCheckedList(SelfCheckInfo info) {
		info.setStatus(BusinessConstants.SELF_CHECK_STATUS_WAIT);
		return selfCheckMapper.selectReviewedSelfCheckList(info);
	}

	@Override
	public int updateCheckListSuccess(List<SelfCheckInfo> info) {
		List<SelfCheckInfo> listCheck = new ArrayList<SelfCheckInfo>();
		List<RepairRecordInfo> list = new ArrayList<RepairRecordInfo>();
		for (int i = 0; i < info.size(); i++) {
			SelfCheckInfo row = info.get(i);
			row.setStatus(BusinessConstants.SELF_CHECK_STATUS_SUCCESS);
			listCheck.add(row);
			Long id = row.getId();
			// 查询最新数据库字段写入记录表
			SelfCheckInfo sfRow = selfCheckMapper.selectSelfCheckListById(id).get(0);
			RepairRecordInfo newRow = new RepairRecordInfo();
			newRow.setSiteKey(sfRow.getSiteKey());
			newRow.setRequestId(id);
			newRow.setRepairCode(sfRow.getRepairCode());
			newRow.setEqKey(sfRow.getEqKey());
			newRow.setRepairUser(sfRow.getRepairUser());
			newRow.setSiteKey(sfRow.getSiteKey());
			newRow.setType(BusinessConstants.REPAIR_RECORD_TYPE_SELF);
			newRow.setStatus(BusinessConstants.REPAIR_RECORD_STATUS_CLOSE);
			list.add(newRow);
		}
		int num = selfCheckMapper.updateSelfCheckInfo(listCheck);
		// 新增维修记录表信息
		repairRecoedMapper.insertPairRecord(list);
		return num;
	}

	@Override
	public int updateCheckListFail(List<SelfCheckInfo> info) {
		List<SelfCheckInfo> listCheck = new ArrayList<SelfCheckInfo>();
		for (int i = 0; i < info.size(); i++) {
			SelfCheckInfo row = info.get(i);
			row.setStatus(BusinessConstants.SELF_CHECK_STATUS_FAIL);
			listCheck.add(row);
		}
		return selfCheckMapper.updateSelfCheckInfo(listCheck);
	}

}
